#include <stdio.h>
#include <signal.h>

#include "include_all.h"

static void runtable_reg(int);
static int runtable_get(int);
static void runtable_unreg(int);
static void errro_report(const char const *,const char const *,const char const *);

static struct watchdog_info *wtd = NULL;

int watchdog_init(struct watchdog_info *watchdog)
{
	wtd = watchdog;
	wtd->run_reg = runtable_reg;
	wtd->run_get = runtable_get;
	wtd->run_unreg = runtable_unreg;
	wtd->report_reg = errro_report;

	wtd->runtable = 0;
	return RET_OK;
}

static void runtable_reg(int tag)
{
	wtd->runtable |= 1 << tag;
}

static int runtable_get(int tag)
{
	return (wtd->runtable & (1 << tag));
}

static void runtable_unreg(int tag)
{
	wtd->runtable &= ~(1 << tag);
}

static void errro_report(const char const *func, const char const *error, const char const *error_info)
{
	if(wtd->run_get(0) > 0)
	{
		printf("\n########\n# Fail # function: %s --> %s", func, error);
		if(error_info != NULL)
		{
			printf(" ===> %s", error_info);
		}
		printf("\n########\n");
		raise(SIGUSR1);
	}
}
